package com.yuhong.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuhong.springbootinit.mapper.PositionMapper;
import com.yuhong.springbootinit.model.entity.Position;
import com.yuhong.springbootinit.model.vo.ApplicationVO;
import com.yuhong.springbootinit.service.ApplicationService;
import com.yuhong.springbootinit.common.ErrorCode;
import com.yuhong.springbootinit.constant.CommonConstant;
import com.yuhong.springbootinit.exception.BusinessException;
import com.yuhong.springbootinit.exception.ThrowUtils;
import com.yuhong.springbootinit.mapper.ApplicationMapper;
import com.yuhong.springbootinit.model.dto.application.ApplicationQueryRequest;
import com.yuhong.springbootinit.model.entity.Application;
import com.yuhong.springbootinit.service.PositionService;
import com.yuhong.springbootinit.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author 10467
* @description 针对表【application】的数据库操作Service实现
* @createDate 2025-02-22 13:52:10
*/
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application>
    implements ApplicationService {

    @Resource
    private PositionMapper positionMapper;

    @Override
    public void validApplication(Application application, boolean add) {
        if (application == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String profileId = application.getProfileId();
        String userId = application.getUserId();
        String positionId = application.getPositionId();
        String companyId = application.getCompanyId();


        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(profileId, companyId, userId,companyId,positionId), ErrorCode.PARAMS_ERROR);
        }
    }

    @Override
    public ApplicationVO getApplicationVO(Application application, HttpServletRequest request) {
        ApplicationVO applicationVO = ApplicationVO.objToVo(application);

        return applicationVO;
    }

    @Override
    public Page<ApplicationVO> getApplicationVOPage(Page<Application> applicationPage, HttpServletRequest request) {

        List<Application> applicationList = applicationPage.getRecords();
        Page<ApplicationVO> applicationVOPage = new Page<>(applicationPage.getCurrent(), applicationPage.getSize(), applicationPage.getTotal());
        if(CollUtil.isEmpty(applicationList)){
            return applicationVOPage;
        }
        List<ApplicationVO> applicationVOS = BeanUtil.copyToList(applicationList, ApplicationVO.class);

        for(ApplicationVO applicationVO : applicationVOS){
            String positionId = applicationVO.getPositionId();
            QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Position::getPositionId,positionId);
            Position position = positionMapper.selectOne(queryWrapper);
            String location = position.getLocation();
            applicationVO.setLocation(location);
        }

        applicationVOPage.setRecords(applicationVOS);

        return applicationVOPage;
    }

    @Override
    public QueryWrapper<Application>  getQueryWrapper(ApplicationQueryRequest applicationQueryRequest) {
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        if (applicationQueryRequest == null) {
            return queryWrapper;
        }

        Long id = applicationQueryRequest.getId();
        String profileId = applicationQueryRequest.getProfileId();
        String positionId = applicationQueryRequest.getPositionId();
        String companyId = applicationQueryRequest.getCompanyId();
        String userAccount = applicationQueryRequest.getUserAccount();
        Integer status = applicationQueryRequest.getStatus();
        String sortField = applicationQueryRequest.getSortField();
        String sortOrder = applicationQueryRequest.getSortOrder();

        // 拼接查询条件
        queryWrapper.like(StringUtils.isNotBlank(profileId), "profile_id", profileId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(positionId), "position_id", positionId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), "status", status);
        queryWrapper.eq(ObjectUtils.isNotEmpty(companyId), "company_id", companyId);

        queryWrapper.eq(ObjectUtils.isNotEmpty(userAccount), "user_id", userAccount);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




